$(function () {
    useLayUI();
})
var search_detail = new Vue({
    el: "#search_detail",
    data() {
        return {
            services: services,
            selectName:"",
            data:null,
            check:true,
            count:0,
        }
    },
    created () {

      
    },
    methods:{
        
        submit: function () {
            var selected = $("#selectSearch").val();
            if(selected == 'position'){
                window.location.href="search-details.html?positionName="+this.selectName ;

            }else if(selected == 'company'){
                window.location.href="search-details.html?companyFullName="+this.selectName ;
            }
        }
    }
});

function getUrlString(name){
    //首先获取到当前页面的地址栏信息
    var url = window.location.href;
    var reg = /\?/;
    obj={}
    if(url.match(reg)) {
        //判断传入参数，以问号截取，问号后是参数
        var chars = url.split('?')[1];

        //再截&号
        var arr = chars.split('&');

        //获得截取后的数组为键值对字符串
        for (var i = 0; i < arr.length; i++) {

            //保守一点确定看是否为 name=value形式
            var num = arr[i].indexOf("=");

            if (num > 0) {
                //拼接字符串
                var key = arr[i].substring(0, num);
                var value = arr[i].substr(num + 1);
                //拼接对象，并转码
                obj[decodeURIComponent(key)] = decodeURIComponent(value);
            }
        }
        if(obj[name]==undefined){
            return null;
        }
       return obj[name];
       
        
    }
    if(obj[name]==undefined){
        return null;
    }
}

var pageIndex = 1;
var pageSize = 10;
var count = 0;
var _url = '/user/position/list';

var positionName;
var flag = 1;
if(getUrlString('positionName')){
    positionName = getUrlString('positionName')
}else if(getUrlString('companyFullName')){
    positionName = getUrlString('companyFullName');
    flag = 2;
}


pagination(pageIndex,positionName, pageSize,'get',_url,flag);
layuipage('search_paging',positionName,pageSize,'get',_url,flag);


//使用layui分页方法
function layuipage(_pageid,positionName, pageSize,_type,_url,flag){
    layui.use('laypage', function () {
        var laypage = layui.laypage;
        laypage.render({
            elem: _pageid
            , count: count
            ,limit:pageSize//每页的条数
            , layout: ['count', 'prev', 'page', 'next', 'skip']
            , jump: function (obj, first) {
                //点击非第一页页码时的处理逻辑。比如这里调用了ajax方法，异步获取分页数据
                if (!first) {
                    pagination(obj.curr,positionName,obj.limit,_type,_url,flag);//第二个参数不能用变量pageSize，因为当切换每页大小的时候会出问题
                }
            }
        });
    });

}

//ajax渲染
function pagination(pageIndex,positionName, pageSize,_type,_url,flag) {
    var param;
    if(flag==1){
        param = {
            page: pageIndex,
            limit: pageSize,
            positionName:positionName,
        };
    }else{
         param = {
             page: pageIndex,
             limit: pageSize,
             companyFullName:positionName,
         };
    }

   
    $.ajax({
        type: _type,
        async:false,
        url: services+_url,
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        data:param,
        success: function (res) {
            var obj = JSON.parse(res);
            search_detail.count = obj.count;
            count = obj.count;
            if(obj.count>0){
                search_detail.data = obj.data;
            }else{
                search_detail.check = false;
            }    
        }
    });
}
